set more off

*******************************************************************************************************************************
*******************************************Figure S.3*****************************************************************************
******************************************************************************************************************************


local N = 54648

local J = 10

set seed 400

do programs/flexiblelogit_point.do `J'  `N'

gen r=.
save point.dta, replace


************************************************************************************************************
*************************Difference in beta absolute value, Figure S.3**************************** 
************************************************************************************************************

use flexlogit
clogit chosen x* z, group(ID)
forvalues i=1/3{
local naivex`i'=_b[x`i']
}
local naivez=_b[z]
keep if _n == 1
keep chosen
forvalues i=1/3 {
gen naivex`i' = `naivex`i''

}
gen naivez=`naivez'
drop chosen
gen r=. 
merge 1:1 r using point
save point,replace  

*file naive.dta are naive logit estimates from Expedia/flexiblelogit_location/bootstrap.dta, it can also be generated alone from bootstrap flexlogit.dta and run simple logit on all choice sets. 

 use bootstrap,clear
 merge 1:1 r using naive
append using point 
drop _merge


forvalues i=1/3 {
gen impliedbeta`i'hatt=impliedbeta`i' if r==.
egen impliedbeta`i'hat=sum(impliedbeta`i'hatt)
drop impliedbeta`i'hatt
}
drop if r==. 
forvalues i=1/3{
sum impliedbeta`i'
gen var`i' =r(Var)
replace var`i'=1/var`i'
}
gen sumvar=var1+var2+var3

forvalues i=1/3{
gen weight`i'=var`i'/sumvar

}
append using point 
forvalues i=1/3{
replace weight`i'=weight`i'[_n-1] if r==.

}
gen waveragebeta=weight1*impliedbeta1+weight2*impliedbeta2+weight3*impliedbeta3 

replace waveragebeta=abs(waveragebeta)-abs(naivez) 

keep r waveragebeta 
gen wbetahatt=waveragebeta if r==. 
egen wbetahat=sum(wbetahatt)
drop wbetahatt 

*z0 

drop if r==.
gen low=(waveragebeta<=wbetahat)
egen sumlow=sum(low)
gen z0=invnormal(sumlow/250)
gen p1=normal(z0+z0-invnormal(0.975))
gen p2=normal(z0+z0+invnormal(0.975))
_pctile waveragebeta, nq(1000)
local p1p=max(1,round(p1*1000))
local p2p=min(999,round(p2*1000))
gen lower=r(r`p1p')
gen upper=r(r`p2p')

keep wbetahat lower upper  
keep if _n==1
rename (wbetahat lower upper)(diffbeta_point diffbeta_lb diffbeta_ub)
label var diffbeta_point "point estimate of the difference between the absolute values of beta estimates from flexible logit and standard logit"
label var diffbeta_lb "lower bound of confidence interval"
label var diffbeta_ub "upper bound of confidence interval"


